package com.xhs.service.impl;

import com.xhs.dto.request.DownloadFileRequest;
import com.xhs.message.Result;
import com.xhs.message.ReturnResult;
import com.xhs.service.DownloadFileService;
import com.xhs.utils.DownloadUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @desc:
 * @projectName: tools-parent
 * @author: xhs
 * @date: 2022/12/3 22:12
 * @version: JDK 1.8
 */
@Slf4j
@Service
public class DownloadFileServiceImpl implements DownloadFileService {

    /**
     * 根据url从服务器下载文件到指定位置
     *
     * @param downloadFileRequest
     * @return
     */
    @Override
    public ReturnResult<Object> downloadFile(DownloadFileRequest downloadFileRequest) {
        String fileUrl = downloadFileRequest.getFileUrl();
        String fileName = downloadFileRequest.getFileName();
        String filePath = downloadFileRequest.getFilePath();
        Integer fileSize = downloadFileRequest.getFileSize();
        String loadFromUrl = null;
        try {
            loadFromUrl = DownloadUtil.downLoadFromUrl(fileUrl, fileName, filePath, fileSize);
        } catch (IOException e) {
            log.error(">>>>>>>>>>>>>>下载文件异常：{}<<<<<<<<<<<<<<<<<<", e.getMessage());
            return ReturnResult.build(Result.DOWNLOAD_FILE_ERROR);
        }
        return ReturnResult.build(Result.DOWNLOAD_FILE_SUCCESS).setData(loadFromUrl);
    }

}
